package com.onix;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.onix.scalecamerapreview.R;
import com.onix.scalecamerapreview.camera.FileUtils;
import com.onix.scalecamerapreview.translate.Language;
import com.onix.scalecamerapreview.translate.OCR.GraphicOverlay;
import com.onix.scalecamerapreview.translate.OCR.MsOcrBean;
import com.onix.scalecamerapreview.translate.OCR.MsWordRecognition;
import com.onix.scalecamerapreview.translate.OCR.Test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class TranslyActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_CHOOSE = 55;
    private EditText inPutEditText,resultText;
    private ImageView imageView;
    private byte[] bytes;
    private GraphicOverlay graphicOverlay;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_transly);

        inPutEditText = (EditText) findViewById(R.id.input);
        resultText = (EditText) findViewById(R.id.result);
        imageView = (ImageView) findViewById(R.id.src);
        graphicOverlay = findViewById(R.id.test);
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String readeLanguageFile = FileUtils.readeLanguageFile();
                Log.d("AAA"," readeLanguageFile "+readeLanguageFile);
            }
        });
        try {
            InputStream inputStream = getAssets().open("test.png");
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            imageView.setImageBitmap(bitmap);
            ViewGroup.LayoutParams layoutParams = graphicOverlay.getLayoutParams();
            layoutParams.height = bitmap.getHeight();
            layoutParams.width = bitmap.getWidth();
            graphicOverlay.setLayoutParams(layoutParams);
            bytes = input2byte(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        findViewById(R.id.request).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MsOcrBean msOcrBean = new Test().execute(graphicOverlay);




//                boolean writeLanguageFile = FileUtils.writeLanguageFile("嘿嘿");
//                Log.d("AAA"," writeLanguageFile "+writeLanguageFile);

            }
        });


    }


    public   byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }


    private void openAlbum() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
      /*  if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE) {
            Observable.create(new ObservableOnSubscribe<Bitmap>() {
                @Override
                public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
                    Bitmap bitmap = ImageUtils.getBitmapFormUri(TranslyActivity.this, data.getData(),720,1280,300);
                    emitter.onNext(bitmap);
                    emitter.onComplete();
                }
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Bitmap>() {
                        @Override
                        public void accept(Bitmap bitmap) throws Exception {
                            imageView.setImageBitmap(bitmap);
                            Log.d("AAA","xx "+bitmap.getHeight()+"-"+bitmap.getWidth());
                        }
                    });
*/
    }


    private String readJsonFile() {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open("translate.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();

    }

    public boolean save(String name,String content){
        boolean sucess = false;
        String docFile = FileUtils.createDocFile();
        if(docFile == null){
            return false;
        }
        String path = docFile+File.separator+name;
        Log.d("AAA","path  "+path);
        File file = new File(path);
        if(file.exists()){
            file.delete();
        }

        try{
            FileOutputStream outStream = new FileOutputStream(file);

            outStream.write(content.getBytes());

            outStream.close();
            Log.d("AAA","Sucess");
        }
        catch (Exception e)
        {
            Log.d("AAA","Error "+e.getMessage());
            sucess = false;
            e.printStackTrace();
        }

        return sucess;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
